﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace homework10
{
    /// <summary>
    /// WinClassEdit.xaml 的交互逻辑
    /// </summary>
    public partial class WinClassEdit : Window
    {
        public WinClassEdit()
        {
            InitializeComponent();
        }

        public School school { get; set; } = new School();
        public Class classToEdit { get; set; } = new Class();
        public int action { get; set; } = 0;

        public bool AddClass(Class xclass)
        {
            try
            {
                using (var context = new AppDbContext())
                {
                    context.Classes.Add(xclass);
                    context.SaveChanges();
                }
                DbHelper.WriteLogs("Add Class:" + xclass.ToString());
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public bool UpdateClass(Class xclass)
        {
            try
            {
                using (var context = new AppDbContext())
                {
                    context.Classes.Update(xclass);
                    context.SaveChanges();
                }
                DbHelper.WriteLogs("Update Class:" + xclass.ToString());
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            txtSchoolID.Text=school.SchoolId.ToString();
            txtSchool.Text = school.Name;
            switch (action)
            {
                case 1:
                    lblCaption.Content = "新增班级";
                    lblOldClass.Visibility = Visibility.Collapsed;
                    txtOldClass.Visibility = Visibility.Collapsed;
                    lblOldClassID.Visibility = Visibility.Collapsed;
                    txtOldClassID.Visibility = Visibility.Collapsed;
                    break;

                case 2:
                    lblCaption.Content = "修改班名";
                    lblOldClassID.Visibility = Visibility.Visible;
                    txtOldClassID.Visibility = Visibility.Visible;
                    lblOldClass.Visibility = Visibility.Visible;
                    txtOldClass.Visibility = Visibility.Visible;
                    txtOldClassID.IsReadOnly = true;
                    txtOldClass.IsReadOnly = true;
                    txtOldClassID.Text = classToEdit.ClassId.ToString();
                    txtOldClass.Text = classToEdit.Name;
                    break;

                default:
                    break;
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string strName = txtClassName.Text;
            strName = strName.Trim();
            if (strName != "")
                switch (action)
                {
                    case 1:
                        AddClass(new Class { SchoolId = school.SchoolId, Name = strName });
                        break;

                    case 2:
                        classToEdit.Name = strName;
                        UpdateClass(classToEdit);
                        break;
                    default: break;
                }
            this.Close();
        }
    }
}
